package demo0914;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPool2 {
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    static class Worker extends Thread {
        private BlockingQueue<Runnable> queue = null;
        public Worker(BlockingQueue<Runnable> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            while(true) {
                try {
                    Runnable runnable = queue.take();
                    runnable.run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    private List<Worker> workers = new ArrayList<>();
    public MyThreadPool2(int num) {
        for (int i = 0; i < num; i++) {
            Worker worker = new Worker(queue);
            worker.start();
            workers.add(worker);
        }
    }

    public void submit (Runnable runnable) {
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}


public class T3fc_0523 {
    public static void main(String[] args) {
        MyThreadPool2 myThreadPool2 = new MyThreadPool2(16);
        for (int i = 0; i < 10; i++) {
            int id = i;
            myThreadPool2.submit(()->{
                System.out.println(id);
            });
        }
    }
}
